package com.gxa.service.impl;

import com.gxa.entity.CitySysOrganization;
import com.gxa.mapper.CitySysOrganizationMapper;
import com.gxa.mapper.OverViewMapper;
import com.gxa.service.OverViewService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class OverViewServiceImpl implements OverViewService {
    @Autowired
    private OverViewMapper overViewMapper;

    @Autowired
    private CitySysOrganizationMapper citySysOrganizationMapper;

    @Override
    public Map findOverViewByDateAndDistrict(String district) {
        int newConfirmedNums = 0;
        int newContactNums = 0;
        int newRemovalNums = 0;
        int newAbnormalNums = 0;
        int commonCasess = 0;
        int CoExaminationCasess = 0;
        int contactNums = 0;
        int secondaryNums = 0;
        int commonNums = 0;
        Map overViewMap = new HashMap();
        Date currentDate = new Date();
        Calendar calendar = Calendar.getInstance();
        // 设置为当前时间
        calendar.setTime(currentDate);
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH)+1;
        String time;
        if (month < 10){
            time = String.valueOf(year)+"-0"+String.valueOf(month);
        }else {
            time = String.valueOf(year)+"-"+String.valueOf(month);
        }

        List timeList = new ArrayList();
        timeList.add(time);

        for (int i=0;i<5;i++){
            calendar.add(Calendar.MONTH,-1);
            year = calendar.get(Calendar.YEAR);
            month = calendar.get(Calendar.MONTH)+1;
            if (month < 10){
                time = String.valueOf(year)+"-0"+String.valueOf(month);
            }else {
                time = String.valueOf(year)+"-"+String.valueOf(month);
            }
            timeList.add(time);
        }

        List confirmedNumList = new ArrayList();

//        Map confirmedMap = new HashMap();

        for (int j = 0;j<timeList.size();j++){

            time = (String)timeList.get(j);

           int confirmedNum =overViewMapper.findConfirmedNumByMonth(time);

            confirmedNumList.add(confirmedNum);

//           confirmedMap.put("月份"+ String.valueOf(j),time);
//           confirmedMap.put("确诊患者数量"+String.valueOf(j),confirmedNum);
        }

            List contactNumList = new ArrayList();
//        Map contactMap = new HashMap();

        for (int j = 0;j<timeList.size();j++){

            time = (String)timeList.get(j);

            int contactNum =overViewMapper.findContactNumByMonth(time);

            contactNumList.add(contactNum);
//            contactMap.put("月份"+ String.valueOf(j),time);
//            contactMap.put("密接人员数量"+String.valueOf(j),contactNum);
        }

        if (district.equals("全部")){

            List<CitySysOrganization> citySysOrganizationList = citySysOrganizationMapper.findCitySysOrganizationList();

            for (CitySysOrganization citySysOrganization : citySysOrganizationList){
                String newDistrict = citySysOrganization.getOrgAddress().substring(0,2);
                int newConfirmedNum = overViewMapper.findNewConfirmedNum(currentDate, newDistrict);
                int newContactNum = overViewMapper.findNewContactNum(currentDate, newDistrict);
                int newRemovalNum = overViewMapper.findNewRemovalNum(currentDate, newDistrict);
                int newAbnormalNum = overViewMapper.findNewAbnormalNum(newDistrict);
                int commonCases = overViewMapper.findCommonCases(newDistrict);
                int CoExaminationCases = overViewMapper.findCoExaminationCases(newDistrict);
                int contactNum = overViewMapper.findcontactNum(newDistrict);
                int secondaryNum = overViewMapper.findsecondaryNum(newDistrict);
                int commonNum = overViewMapper.findcommonNum(newDistrict);

                newConfirmedNums += newConfirmedNum;
                newContactNums += newContactNum;
                newRemovalNums += newRemovalNum;
                newAbnormalNums += newAbnormalNum;
                commonCasess += commonCases;
                CoExaminationCasess += CoExaminationCases;
                contactNums += contactNum;
                secondaryNums += secondaryNum;
                commonNums += commonNum;
            }
            overViewMap.put("今日新增确诊病例数（人）", newConfirmedNums);
            overViewMap.put("今日新增密接人员（人）", newContactNums);
            overViewMap.put("今日解除隔离（人）", newRemovalNums);
            overViewMap.put("异常预警数（个）", newAbnormalNums);
            overViewMap.put("普通病例数", commonCasess);
            overViewMap.put("协查病例数", CoExaminationCasess);
            overViewMap.put("密接人员总数", contactNums);
            overViewMap.put("次密接人员总数", secondaryNums);
            overViewMap.put("一般接触人员总数", commonNums);
        }else {
            String newDistrict = district.substring(0, 2);
            newConfirmedNums = overViewMapper.findNewConfirmedNum(currentDate, newDistrict);
            newContactNums = overViewMapper.findNewContactNum(currentDate, newDistrict);
            newRemovalNums = overViewMapper.findNewRemovalNum(currentDate, newDistrict);
            newAbnormalNums = overViewMapper.findNewAbnormalNum(newDistrict);
            commonCasess = overViewMapper.findCommonCases(newDistrict);
            CoExaminationCasess = overViewMapper.findCoExaminationCases(newDistrict);
            contactNums = overViewMapper.findcontactNum(newDistrict);
            secondaryNums = overViewMapper.findsecondaryNum(newDistrict);
            commonNums = overViewMapper.findcommonNum(newDistrict);

            overViewMap.put("今日新增确诊病例数（人）", newConfirmedNums);
            overViewMap.put("今日新增密接人员（人）", newContactNums);
            overViewMap.put("今日解除隔离（人）", newRemovalNums);
            overViewMap.put("异常预警数（个）", newAbnormalNums);
            overViewMap.put("普通病例数", commonCasess);
            overViewMap.put("协查病例数", CoExaminationCasess);
            overViewMap.put("密接人员总数", contactNums);
            overViewMap.put("次密接人员总数", secondaryNums);
            overViewMap.put("一般接触人员总数", commonNums);
        }
        Map resultMap = new HashMap();

//        resultMap.put("confirmedMap",confirmedMap);
//        resultMap.put("contactMap",contactMap);
        resultMap.put("overViewMap",overViewMap);

        resultMap.put("timeList",timeList);
        resultMap.put("confirmedNumList",confirmedNumList);
        resultMap.put("contactNumList",contactNumList);
        return resultMap;
    }
}
